package com.fire.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.fire.definedAnnotation.Authorization;
import com.fire.service.SystemService;
import com.fire.util.MsgInfo;
import com.fire.util.base.ResultBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/system")
@Api(description = "系统模块API")
public class SystemController extends BaseController {

	@Autowired
	private SystemService systemService;

	/**
	 * 供应商登录接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "用户登录", notes = "供应商通过此API登录系统")
	@ApiImplicitParam(name = "json入参", value = "phoneNum,password", required = true, dataType = "String")
	@Authorization
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public ResultBody login(HttpServletRequest request, HttpServletResponse response) {
		ResultBody result = null;
		JSONObject params = super.initJsonParam(request);
		try {
			result = systemService.login(params);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

	/**
	 * 供应商注册接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@Authorization
	@ApiOperation(value = "用户注册", notes = "新供应商通过此API注册手机号")
	@ApiImplicitParam(name = "json入参", value = "phoneNum,password", required = true, dataType = "String")
	@RequestMapping(value = "/register.do", method = RequestMethod.POST)
	public ResultBody register(HttpServletRequest request, HttpServletResponse response) {
		ResultBody result = null;
		JSONObject params = super.initJsonParam(request);
		try {
			result = systemService.register(params);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

	/**
	 * 重置密码接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@Authorization
	@ApiOperation(value = "重置密码", notes = "新供应商通过此API重新设置密码")
	@ApiImplicitParam(name = "json入参", value = "phoneNum,password", required = true, dataType = "String")
	@RequestMapping(value = "/resetPwd.do", method = RequestMethod.POST)
	public ResultBody resetPwd(HttpServletRequest request, HttpServletResponse response) {
		ResultBody result = null;
		JSONObject params = super.initJsonParam(request);
		try {
			result = systemService.resetPwd(params);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

	/**
	 * 发送验证码接口
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@Authorization
	@ApiOperation(value = "验证码", notes = "通过此API获取手机验证码")
	@ApiImplicitParam(name = "json入参", value = "phoneNum", required = true, dataType = "String")
	@RequestMapping(value = "/sendSmsCode.do", method = RequestMethod.POST)
	public ResultBody sendSmsCode(HttpServletRequest request, HttpServletResponse response) {
		ResultBody result = null;
		JSONObject params = super.initJsonParam(request);
		try {
			result = systemService.sendSmsCode(params);
		} catch (Exception e) {
			result = ResultBody.goBack(MsgInfo.z_error_code, MsgInfo.z_error_msg);
			e.printStackTrace();
			return result;
		}
		return result;
	}

}
